<?php

namespace App\Models\Es;

use Carbon\Carbon;

class EsAssets20220421Model extends EsBaseModel
{
    //index
    protected $table = 'assets20220421';

    protected $guarded = [];

    public const SOURCE_EAM = 1;
    //端口类型 开放端口
    public const PORT_FLG_OPEN = 1;
    //WEB APP
    public const PORT_FLG_WEB_APP = 2;
    //APP
    public const PORT_FLG_APP = 3;
    //MIDDLEWARE
    public const PORT_FLG_MIDDLEWARE = 4;
    //DATABASE
    public const PORT_FLG_DATABASE = 5;


    public function scopeSearch($query, $search)
    {
        return $query->when(!is_null($search['status'] ?? null), function ($q) use ($search) {
            return $q->where('status', $search['status']);
        });
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $field
     * @return mixed
     * 查询资产大屏-资产类型统计
     */
    public static function esSelectCount($es, $assetsEs, $field, $where, $size = 50000)
    {
        $aggregations = "terms_" . $field;
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                "size" => 0,
                "query" => [
                    "bool" => [
                        "filter" => $where
                    ]
                ],
                "aggs" => [
                    $aggregations => [
                        "terms" => [
                            "field" => $field,
                            "size" => $size,
                            "order" => [
                                "_count" => "desc"
                            ],
                        ]
                    ]
                ]
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $superType
     * @param $field
     * @param string $orderBy
     * @param int $size
     * @return mixed
     * 获取详情，分组数据，然后倒排序
     */
    public static function esSelectAssetDetailsCount($es, $assetsEs, $field, $where, $orderBy = "desc", $size = 10)
    {
        $aggregations = "terms_" . $field;
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                "size" => 0,
                "query" => [
                    "bool" => [
                        "filter" => $where
                    ]
                ],
                "aggs" => [
                    $aggregations => [
                        "terms" => [
                            "field" => $field,
                            "order" => [
                                "_count" => $orderBy
                            ],
                            "size" => $size,
                        ]
                    ],
                ]
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $superType
     * @param $data
     * @return mixed
     * 资产大屏-安全产品分类构成-详情页面 数据
     */
    public static function esSelectSafetySystemClassDetailsCount($es, $assetsEs, $where, $data)
    {
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                "size" => 0,
                "query" => [
                    "bool" => [
                        "filter" => $where
                    ]
                ],
                "aggs" => [
                    $data[0] . "_group" => [
                        "terms" => [
                            "field" => $data[0],
                            "size" => 50000
                        ],
                        "aggs" => [
                            $data[1] . "_group" => [
                                "terms" => [
                                    "field" => $data[1],
                                    "size" => 50000
                                ],
                            ]
                        ]
                    ]
                ]
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $field
     * @param $where
     * @param $size
     * @return mixed
     * 资产大屏-资产品牌排名前10
     */
    public static function esSelectAssetBrandTop($es, $assetsEs, $field, $where, $size = 50000)
    {
        $aggregations = "terms_" . $field;
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                'size' => 0,
                "query" => [
                    "bool" => [
                        'filter' => $where,
                    ],
                ],
                "aggs" => [
                    $aggregations => [
                        "nested" => [
                            "path" => "model",
                        ],
                        'aggs' => [
                            $field . '_group' => [
                                'terms' => [
                                    'field' => "model.model.keyword",
                                    "size" => $size,
                                    'exclude' => [""],
                                    "order" => [
                                        "_count" => "desc"
                                    ],
                                ],
                            ],
                        ],
                    ]
                ]
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $where
     * @param $field
     * @return mixed
     * 资产大屏-资产品牌排名前10
     */
    public static function esSelectAssetBrandTopModelNested($es, $assetsEs, $where, $field)
    {
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                'size' => 0,
                "query" => [
                    "bool" => [
                        'must' => [
                            'nested' => [
                                'path' => 'model',
                                'query' => [
                                    'bool' => [
                                        'should' => $where
                                    ]
                                ]
                            ]
                        ],
                    ],
                ],
                "aggs" => [
                    $field . "_group" => [
                        "terms" => [
                            "field" => $field,
                            'exclude' => [""],
                            "size" => 50000
                        ],
                    ],
                ]
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $where
     * @return mixed
     * 资产大屏-资产品牌排名前10-详情
     */
    public static function esSelectAssetBrandTopDetails($es, $assetsEs, $where)
    {
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                'size' => 0,
                "query" => [
                    "bool" => [
                        'must' => [
                            'nested' => [
                                'path' => 'model',
                                'query' => [
                                    'bool' => [
                                        'should' => $where
                                    ]
                                ]
                            ]
                        ],
                    ],
                ],
                "aggs" => [
                    'company_name_group' => [
                        'terms' => [
                            'field' => 'company_name.keyword',
                            'exclude' => [""],
                            "size" => 50000
                        ],
                        'aggs' => [
                            "super_type_group" => [
                                "terms" => [
                                    "field" => 'super_type',
                                    'exclude' => [""],
                                    "size" => 50000
                                ],
                            ],
                        ]
                    ],
                ]
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $where
     * @param $limit
     * @return mixed
     * 资产大屏-资产实时发现
     */
    public static function esSelectAssetTimelyFindList($es, $assetsEs, $where, $limit = 5)
    {
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                "size" => 0,
                "query" => [
                    "bool" => [
                        "filter" => $where
                    ]
                ],
                'aggs' => [
                    'city_code_group' => [
                        "terms" => [
                            "field" => 'city_code',
                            'size' => 50000,
                        ],
                        "aggs" => [
                            "isp_code_group" => [
                                "terms" => [
                                    "field" => 'isp_code',
                                    'exclude' => [""],
                                    'size' => 50000,
                                ]
                            ]
                        ]
                    ]
                ],
            ]
        ]);
    }

    /**
     * @param $es
     * @param $assetsEs
     * @param $where
     * @return mixed
     * 资产大屏-大屏标题下的数据展示
     */
    public static function esSelectAssetHomeDataShowList($es, $assetsEs, $where, $limit = 5)
    {
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                "size" => 0,
                "query" => [
                    "bool" => [
                        "filter" => $where
                    ]
                ],
                "aggs" => [
                    'super_type' => [
                        "terms" => [
                            "field" => 'super_type',
                            "size" => 10000,
                            "exclude" => [""]
                        ],
                        'aggs' => [
                            "company_name" => [
                                'terms' => [
                                    'field' => 'company_name',
                                    "exclude" => [""]
                                ],
                            ]
                        ]
                    ],
                ]
            ]
        ]);
    }
}
